package Json;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.annotation.JSONField;

public class JsonTest {

    public static void main(String[] args) {
        List<Person> listOfPersons = new ArrayList<Person>();
        listOfPersons.add(new Person(15, "John Doe", new Date(), "男", "John", "Doe"));
        listOfPersons.add(new Person(20, "Janette Doe", new Date(), "女", "Janette", "Doe"));

        System.out.println("集合序列化");
        String jsonOutput = JSON.toJSONString(listOfPersons);// 序列化
        System.out.println("Json:");
        System.out.println(jsonOutput);

        System.out.println("集合反序列化");
        List<Person> persons = JSON.parseArray(jsonOutput, Person.class);// 反序列化集合
        System.out.println(persons);

        System.out.println("对象序列化");
        jsonOutput = JSON.toJSONString(persons.get(0));// 序列化
        System.out.println("Json:");
        System.out.println(jsonOutput);

        System.out.println("对象反序列化");
        Person person = JSON.parseObject(jsonOutput, Person.class);// 反序列化集合
        System.out.println(person);
    }
}

class Person {

    @JSONField(name = "FULL NAME")
    private String fullName;

    /**
     * 使用 transient 指定字段不序列化
     */
    @JSONField(name = "Sex")
    private transient String sex;

    /**
     * 使用 serialize 指定字段不序列化
     */
    @JSONField(name = "AGE", serialize = false)
    private int age;

    /**
     * ordinal 参数指定字段的顺序
     */
    @JSONField(name = "LAST NAME", ordinal = 2)
    private String lastName;

    @JSONField(name = "FIRST NAME", ordinal = 1)
    private String firstName;

    @JSONField(name = "DATE OF BIRTH", format = "yyyy-MM-dd", ordinal = 3)
    private Date dateOfBirth;

    public Person() {
        super();
    }

    public Person(int age, String fullName, Date dateOfBirth, String sex, String lastName, String firstName) {
        super();
        this.age = age;
        this.fullName = fullName;
        this.dateOfBirth = dateOfBirth;
        this.sex = sex;
        this.lastName = lastName;
        this.firstName = firstName;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(String name) {
        this.fullName = name;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Date getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    @Override
    public String toString() {
        return "全名：" + fullName + ",年龄" + age + ",性别：" + sex + ",生日：" + dateOfBirth;
    }
}
